﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


class PistonScript : EnhancementBlock
{
    Rigidbody Rigidbody;
    ConfigurableJoint myJoint;
    AnimationCurve animationCurve;

    public override void SafeAwake()
    {
#if DEBUG
        Debug.Log("活塞添加摇杆");
#endif
    }

    public override void ChangedProperties()
    {
        base.ChangedProperties();
    }

    SliderCompress sliderCompress;
    public override void OnSimulateStart_EnhancementEnabled()
    {
        Rigidbody = GetComponent<Rigidbody>();
        myJoint = GetComponent<ConfigurableJoint>();
        sliderCompress = GetComponent<SliderCompress>();

        //animationCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0f + joyAxis.Offset, sliderCompress.startLimit), new Keyframe(/*-1f * joyAxis.Sign*/1f*joyAxis.Max, sliderCompress.newLimit) });
    }
    float targetPos = 0f;
    public override void SimulateUpdateAlways_EnhancementEnable()
    {
        var value = 0f;
        if (LerpToggle.IsActive)
        {
            value = Mathf.MoveTowards(targetPos, /*animationCurve.Evaluate(*/joyAxis.Value/*)*/, LerpSpeedSlider.Value * Time.deltaTime);
        }
        else
        {
            value =/* animationCurve.Evaluate(*/joyAxis.Value/*)*/;
        }

        targetPos = value;
        sliderCompress.posToBe = targetPos;
        float single = this.myJoint.targetPosition.x;
        if (single != sliderCompress.posToBe && this.myJoint.connectedBody != null)
        {
            if (this.Rigidbody.IsSleeping())
            {
                this.Rigidbody.WakeUp();
            }
            if (this.myJoint.connectedBody.IsSleeping())
            {
                this.myJoint.connectedBody.WakeUp();
            }
            ConfigurableJoint vector3 = this.myJoint;
            float single1 = Mathf.Lerp(single, sliderCompress.posToBe, Time.deltaTime * sliderCompress.lerpSpeed * sliderCompress.SpeedSlider.Value);
            float single2 = this.myJoint.targetPosition.y;
            Vector3 vector31 = this.myJoint.targetPosition;
            vector3.targetPosition = new Vector3(single1, single2, vector31.z);
        }

    }
}

